package com.justnow.offer;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * @author justnow
 * Created on 2020-09-01
 * Description
 */
public class Solution1 {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        List<Integer> list = new ArrayList<>();
        int N = scanner.nextInt();
        int M = scanner.nextInt();
        int[][] array = new int[N+1][M+1];
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < M; j++) {
                int temp = scanner.nextInt();
                list.add(temp);
                array[i][j] = temp;
            }
        }

        int max = Integer.MIN_VALUE;

        for (int i = 0; i < list.size(); i++) {
            for (int j = i + 1; j < list.size(); j++) {
                int start = array[i / M][i % M];
                int end = array[j / M][j % M];
                if (start == 0 || end == 0) {
                    continue;
                }

                int temp = array[i / M][i % M];
                array[i / M][i % M] = array[j / M][j % M];
                array[j / M][j % M] = temp;
                for (int o = 0; o < N; o++) {
                    for (int p = 0; p < M; p++) {
                        int value = dfs(array, o, p, 0);
                        max = Math.max(max, value);
                    }
                }
            }
        }


        //how to change



        System.out.println(max);

    }

    public static int dfs(int[][] array, int i, int j, int k) {
        if (i >= array.length || i < 0 || j >= array[0].length || j < 0 || array[i][j] == 0 || array[i][j] == -1) {
            return k;
        }

        k++;
        int tmp = array[i][j];
        array[i][j] = -1;

        int top = dfs(array, i - 1, j, k);
        int left = dfs(array, i, j - 1, k);
        int down = dfs(array, i + 1, j, k);
        int right = dfs(array, i, j + 1, k);
        int max1 = Math.max(top, left);
        int max2 = Math.max(down, right);
        int max = Math.max(max1, max2);
        array[i][j] = tmp;
        return max;

    }


}
